﻿using System;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using System.Windows;
using System.Windows.Interactivity;

namespace OpenLightGroup.Common.Interactivity
{
    /// <summary>
    /// Invokes the associated actions when the current user logs out.
    /// </summary>
    /// <remarks>
    /// Uses WebContextBase.Current.Authentication.LoggedOut event.
    /// </remarks>
    public class UserLoggedOutTrigger : TriggerBase<DependencyObject>
    {

        /// <summary>
        /// Writes up the event handler to WebContextBase.Current.Authentication.LoggedOut
        /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();
            WebContextBase.Current.Authentication.LoggedOut += new EventHandler<AuthenticationEventArgs>(Authentication_LoggedOut);
        }

        /// <summary>
        /// Removes the event handler to WebContextBase.Current.Authentication.LoggedOut
        /// </summary>
        protected override void OnDetaching()
        {
            base.OnDetaching();
            WebContextBase.Current.Authentication.LoggedOut -= new EventHandler<AuthenticationEventArgs>(Authentication_LoggedOut);
        }

        private void Authentication_LoggedOut(object sender, AuthenticationEventArgs e)
        {
            this.InvokeActions(this);
        }



    }
}